<?
/*******************************************
IDENTIFICATION WGM
v1.0
*******************************************/

$G_cookie="WGM";
$G_cookie_expire=time()+(30*86400);

function ident_init(){
	global $sql, $G_cookie, $G_ccokie_expire;

	if(isset($_COOKIE[$G_cookie])&&($_COOKIE[$G_cookie]!="")&&(!isset($_SESSION["user"]))){
		$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."user where id=".$_COOKIE[$G_cookie],0);
		if($var!==false){
			$user=new user($var);
			$user->init_groups();
			$_SESSION["user"]=serialize($user);
		}
	}

	if(!isset($_SESSION["user"])){
		$_SESSION["user"]=serialize(new user(array("id"=>0,"nom"=>"Invité","groupes"=>"0","avatar"=>"")));
	}
}


function ident_login($login,$mdp){
	global $sql, $G_cookie, $G_ccokie_expire;
	
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."user where login='".$login."' and mdp='".$mdp."'");
	if($var!==false){
		$user=new user($var);
		$user->init_groups();
		$_SESSION["user"]=serialize($user);
		setcookie($G_cookie,$user->id,$G_cookie_expire);
	}
}

function ident_get_users_list(){
	global $sql;
	
	$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."user order by nom");
	$tmp=array();
	foreach($res as $var)$tmp[]=new user($var);
	return $tmp;
}

function ident_get_user($id){
	global $sql;
	
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."user where id=".$id);
	if($var===false) return false;
	else return new user($var);
}

function ident_set_datas(){
	$GLOBALS["DATA"]["USER_ID"]=$GLOBALS["USER"]->id;
	$GLOBALS["DATA"]["USER_NOM"]=$GLOBALS["USER"]->nom;
	$GLOBALS["DATA"]["USER_AVATAR"]=$GLOBALS["USER"]->avatar;
}


